package com.jxs.android.cmcc.boot;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.cmcc.data.UserInfo;
import com.jxs.android.weiget.PopMsg;

@SuppressLint("HandlerLeak")
public class SelfApplication extends Application {
	public static final int ERROR_MSG = 0x9999;

	// public static String WSDL_URL =
	// "http://211.138.97.22:8080/mmsws/services/MobileService"; // 山西正式库地址
	public static String WSDL_URL = "http://60.164.227.235:9999/icloud/services/BranchService"; // 测试库地址
	public static String WSDL_NAME_SPACE = "http://service.branch.apps.com/";

	private static SelfApplication application;

	private static UserInfo userInfo = null;
	private static String dataReadModel = "1";

	public SelfApplication() {
		mLowMemoryListeners = new ArrayList<WeakReference<OnLowMemoryListener>>();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		setApplication(this);
	}

	public Handler handler = new Handler() {

		@SuppressLint({ "HandlerLeak", "HandlerLeak" })
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == ERROR_MSG) {
				PopMsg.show("服务器数据异常，请稍后尝试...", 3000);
			}
		}

	};

	public static SelfApplication getApplication() {
		return application;
	}

	public static void setApplication(SelfApplication application) {
		SelfApplication.application = application;
	}

	public static UserInfo getUserInfo() {
		if (null == userInfo) {
			userInfo = new UserInfo();
		}
		return userInfo;
	}

	public static void setUserInfo(UserInfo userInfo) {
		SelfApplication.userInfo = userInfo;
	}

	/**
	 * Used for receiving low memory system notification. You should definitely
	 * use it in order to clear caches and not important data every time the
	 * system needs memory.
	 * 
	 * @author Cyril Mottier
	 * @see GDApplication#registerOnLowMemoryListener(OnLowMemoryListener)
	 * @see GDApplication#unregisterOnLowMemoryListener(OnLowMemoryListener)
	 */
	static public interface OnLowMemoryListener {

		/**
		 * Callback to be invoked when the system needs memory.
		 */
		public void onLowMemoryReceived();
	}

	private static final int CORE_POOL_SIZE = 5;

	private static final ThreadFactory sThreadFactory = new ThreadFactory() {
		private final AtomicInteger mCount = new AtomicInteger(1);

		public Thread newThread(Runnable r) {
			return new Thread(r, "GreenDroid thread #" + mCount.getAndIncrement());
		}
	};

	private ExecutorService mExecutorService;
	private ArrayList<WeakReference<OnLowMemoryListener>> mLowMemoryListeners;

	/**
	 * Return an ExecutorService (global to the entire application) that may be
	 * used by clients when running long tasks in the background.
	 * 
	 * @return An ExecutorService to used when processing long running tasks
	 */
	public ExecutorService getExecutor() {
		if (mExecutorService == null) {
			mExecutorService = Executors.newFixedThreadPool(CORE_POOL_SIZE, sThreadFactory);
		}
		return mExecutorService;
	}

	/**
	 * Return this application {@link ImageCache}.
	 * 
	 * @return The application {@link ImageCache}
	 */

	/**
	 * Return the class of the home Activity. The home Activity is the main
	 * entrance point of your application. This is usually where the
	 * dashboard/general menu is displayed.
	 * 
	 * @return The Class of the home Activity
	 */
	public Class<?> getHomeActivityClass() {
		return null;
	}

	/**
	 * Each application may have an "application intent" which will be used when
	 * the user clicked on the application button.
	 * 
	 * @return The main application Intent (may be null if you don't want to use
	 *         the main application Intent feature)
	 */
	public Intent getMainApplicationIntent() {
		return null;
	}

	/**
	 * Add a new listener to registered {@link OnLowMemoryListener}.
	 * 
	 * @param listener
	 *            The listener to unregister
	 * @see OnLowMemoryListener
	 */
	public void registerOnLowMemoryListener(OnLowMemoryListener listener) {
		if (listener != null) {
			mLowMemoryListeners.add(new WeakReference<OnLowMemoryListener>(listener));
		}
	}

	/**
	 * Remove a previously registered listener
	 * 
	 * @param listener
	 *            The listener to unregister
	 * @see OnLowMemoryListener
	 */
	public void unregisterOnLowMemoryListener(OnLowMemoryListener listener) {
		if (listener != null) {
			int i = 0;
			while (i < mLowMemoryListeners.size()) {
				final OnLowMemoryListener l = mLowMemoryListeners.get(i).get();
				if (l == null || l == listener) {
					mLowMemoryListeners.remove(i);
				} else {
					i++;
				}
			}
		}
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		int i = 0;
		while (i < mLowMemoryListeners.size()) {
			final OnLowMemoryListener listener = mLowMemoryListeners.get(i).get();
			if (listener == null) {
				mLowMemoryListeners.remove(i);
			} else {
				listener.onLowMemoryReceived();
				i++;
			}
		}
	}

	public static String getDataReadModel() {
		return dataReadModel;
	}

	public static void setDataReadModel(String dataReadModel) {
		SelfApplication.dataReadModel = dataReadModel;
	}

}
